package org.ala.tcp_feign_common.enums;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 序列化方式
 *
 * @author 骆毅(ala)
 * @date 2024年4月18日
 */
public enum TcpFeignSerializableTypeEnums {

	
	JSON((byte)1, "json"),
	KRYO((byte)2, "kryo"),
	CUSTOM((byte)3, "custom")
	;
	private byte val;
	private String code;
	private TcpFeignSerializableTypeEnums(byte val, String code) {
		this.val = val;
		this.code = code;
	}
	
	
	static Map<Byte, TcpFeignSerializableTypeEnums> valMapper;
	public static TcpFeignSerializableTypeEnums val(byte val) {
		if (valMapper == null) {
			synchronized (TcpFeignSerializableTypeEnums.class) {
				if (valMapper == null) {
					valMapper = Arrays.stream(values()).collect(Collectors.toMap(v->v.getVal(), v->v));
				}
			}
		}
		return valMapper.get(val);
	}
	
	
	static Map<String, TcpFeignSerializableTypeEnums> codeMapper;
	public static TcpFeignSerializableTypeEnums code(String code) {
		if (codeMapper == null) {
			synchronized (TcpFeignSerializableTypeEnums.class) {
				if (codeMapper == null) {
					codeMapper = Arrays.stream(values()).collect(Collectors.toMap(v->v.getCode(), v->v));
				}
			}
		}
		return codeMapper.get(code);
	}
	public byte getVal() {
		return val;
	}
	public void setVal(byte val) {
		this.val = val;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
}
